define(['jquery', 'bootstrap', 'backend', 'table', 'form','editable'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
					xedit_url: 'user/user/xedit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
					freeze_url:'user/user/freeze',
					unfreeze_url:'user/user/unfreeze',
                    table: 'user',
                }
            });
            
            //绑定事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var panel = $($(this).attr("href"));
                if (panel.length > 0) {
                    Controller.table[panel.attr("id")].call(this);
                    $(this).on('click', function (e) {
                        $($(this).attr("href")).find(".btn-refresh").trigger("click");
                    });
                }
                //移除绑定的事件
                $(this).unbind('shown.bs.tab');
            });
            
            //必须默认触发shown.bs.tab事件
            $('ul.nav-tabs li.active a[data-toggle="tab"]').trigger("shown.bs.tab");
        },
        table: {
            first: function () {
                // 表格1
                var table1 = $("#table1");
                 // 初始化表格
				table1.bootstrapTable({
					url: 'user/user/table1',
                    toolbar: '#toolbar1',
                    sortName: 'id',
					pk: 'id',
                    search: true,
					fixedColumns:true,   //固定列代码
					fixedRightNumber: 1, 
					columns: [
						[
							{checkbox: true},
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'username', title: __('Username'), operate: 'LIKE'},
							{field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
							{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
							{
								field: 'money', 
								title: __('余额'), 
								operate: 'BETWEEN',
								editable: {
								  type: 'text',
								  title: '修改余额',
								  validate: function (value) {
									  if (!$.trim(value)) {
											 return '不能为空';
										 }
									  }
								},
								searchable:false
								
							},
							{field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
							{field: 'real_name', title: __('真实姓名'), operate: 'LIKE',searchable:false},
							{field: 'id_card', title: __('身份号'), operate: 'LIKE',searchable:false},
							{field: 'bank_num', title: __('银行卡号'), operate: 'LIKE',searchable:false},
							{field: 'bank_name', title: __('银行名称'), operate: 'LIKE',searchable:false},
							{field: 'bank_sub_name', title: __('支行名称'), operate: 'LIKE',searchable:false},

							{field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true,searchable:false},
							{field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true,searchable:false},
							{field: 'jointime', title: __('注册时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{field: 'examine_time', title: __('实名时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,searchable:false},
							{field: 'jointime', title: __('绑定银行卡时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,searchable:false},
							{field: 'prevtime', title: __('Prevtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true,searchable:false},
							{
								field: 'status', 
								title: __('Status'), 
								formatter: Table.api.formatter.status, 
								searchList: {normal: __('Normal'), freeze: __('已冻结')},
								custom:{normal: 'success', freeze: 'red'},
								searchable:false
							},
							{
								field: 'operate', 
								title: __('Operate'), 
								table: table1, 
								events: Table.api.events.operate, 
								formatter: function (value, row, index) {
								  var that = $.extend({}, this);
								  var table = $(that.table).clone(true);
								  if( row.status == 'freeze' ){
									  $(table).data("operate-freeze", null);
								  }
								  that.table = table;
								  return Table.api.formatter.operate.call(that, value, row, index);
								},
								buttons: [
									{
										name: 'freeze',
										title: __('账户冻结'),
										classname: 'btn btn-xs btn-primary btn-dialog',
										icon: 'fa fa-lock',
										url:'user/user/freeze'
									},
								]
							}
						]
					]
				});

                // 为表格1绑定事件
                Table.api.bindevent(table1);
            },
            second: function () {
                // 表格2
                var table2 = $("#table2");
                table2.bootstrapTable({
                    url: 'user/user/table2',
                    toolbar: '#toolbar2',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'username', title: __('Username'), operate: 'LIKE'},
							{field: 'nickname', title: __('Nickname'), operate: 'LIKE',searchable:false},
							{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
							{field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
							{field: 'freeze_time', title: __('冻结时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{field: 'freeze_reason', title: __('冻结原因'), operate: 'LIKE',searchable:false},
							{field: 'freeze_notice', title: __('会员提示信息'), operate: 'LIKE',searchable:false},
							{
								field: 'status', 
								title: __('Status'), 
								formatter: Table.api.formatter.status, 
								searchList: {normal: __('Normal'), freeze: __('已冻结')},
								custom:{normal: 'success', freeze: 'red'},
								searchable:false
							},
							{
								field: 'operate', 
								title: __('Operate'), 
								table: table2, 
								events: Table.api.events.operate, 
								formatter: function (value, row, index) {
									var that = $.extend({}, this);
									var table = $(that.table).clone(true);
									$(table).data("operate-del", null);
									$(table).data("operate-edit", null);
									that.table = table;
									return Table.api.formatter.operate.call(that, value, row, index);
								},
								buttons: [
									{
										name: 'unfreeze',
										title: __('解冻'),
										classname: 'btn btn-xs btn-primary btn-dialog',
										icon: 'fa fa-fire',
										url:'user/user/unfreeze'
									},
								]
							}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table2);
            },
			three: function () {
                // 表格2
                var table3 = $("#table3");
                table3.bootstrapTable({
                    url: 'user/user/table3',
                    extend: {
                        index_url: '',
                        add_url: '',
                        edit_url: '',
                        del_url: '',
                        multi_url: '',
                        table: '',
                    },
                    toolbar: '#toolbar3',
                    sortName: 'id',
                    search: false,
                    columns: [
                        [
							{field: 'id', title: __('Id'), sortable: true},
							{field: 'username', title: __('Username'), operate: 'LIKE'},
							{field: 'nickname', title: __('Nickname'), operate: 'LIKE',searchable:false},
							{field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
							{field: 'freeze_time', title: __('冻结时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', autocomplete:false,searchable:false},
							{field: 'freeze_reason', title: __('冻结原因'), operate: 'LIKE',searchable:false},
							{field: 'freeze_notice', title: __('会员提示信息'), operate: 'LIKE',searchable:false},
							{field: 'unfreeze_time', title: __('解冻时间'), operate: 'LIKE',searchable:false},
							{field: 'unfreeze_reason', title: __('解冻原因'), operate: 'LIKE',searchable:false},
							{
								field: 'status', 
								title: __('Status'), 
								formatter: Table.api.formatter.status, 
								searchList: {0: __('已解冻'), 1: __('冻结中')},
								custom:{0: 'success', 1: 'red'},
								searchable:false
							}
                        ]
                    ]
                });

                // 为表格2绑定事件
                Table.api.bindevent(table3);
            }
			
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
		freeze: function () {
            Controller.api.bindevent();
        },
		unfreeze: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
			
			events:{
				custom: {
                    'change .btn-browser': function (e, value, row, index) {
                        e.stopPropagation();
                        Layer.alert("该行数据为: <code>" + JSON.stringify(row) + "</code>");
                    }
                },
			}
        }
    };
    return Controller;
});
